package com.mkyong;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;

import com.mkyong.utils.SortableDataModel;

@ManagedBean(name = "order")
@SessionScoped
public class OrderBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private String orderNo;
	private String productName;
	private BigDecimal price;
	private int qty;
	private boolean sortAscending = true;
	private SortableDataModel<Order> sortableDataModel;



	public OrderBean() {
		sortableDataModel = new SortableDataModel<Order>(
				new ArrayDataModel<Order>(orderArray));
	}

	public DataModel<Order> getSortableDataModelOrderList() {
		return sortableDataModel;
	}

	public String sortByOrderNo() {
		if (sortAscending) {
			Collections.sort(orderList, new Comparator<Order>() {
				public int compare(Order o1, Order o2) {
					return o1.getOrderNo().compareTo(o2.getOrderNo());
				}
			});
			sortAscending = false;
		} else {
			Collections.sort(orderList, new Comparator<Order>() {
				public int compare(Order o1, Order o2) {
					return o2.getOrderNo().compareTo(o1.getOrderNo());
				}
			});
			sortAscending = true;
		}
		return null;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	private static Order[] orderArray = new Order[] {
			new Order("A0001", "Intel CPU", new BigDecimal("700.00"), 1),
			new Order("A0002", "Harddisk 10TB", new BigDecimal("500.00"), 2),
			new Order("A0003", "Dell Laptop", new BigDecimal("11600.00"), 8),
			new Order("A0004", "Samsung LCD", new BigDecimal("5200.00"), 3),
			new Order("A0005", "A4Tech Mouse", new BigDecimal("100.00"), 10) };

	public Order[] getOrderArray() {
		System.out.println(Arrays.toString(orderArray));
		return orderArray;
	}

	private static List<Order> orderList = new ArrayList<Order>(
			Arrays.asList(orderArray));

	public List<Order> getOrderList() {
		return orderList;
	}

	private DataModel<Order> order = new ArrayDataModel<Order>(orderArray);

	public DataModel<Order> getOrderDataModelList() {
		return order;
	}

	public String addAction() {
		Order newOrder = new Order(this.orderNo, this.productName, this.price,
				this.qty);
		orderList.add(newOrder);
		return null;
	}

	public String deleteAction(Order order) {
		boolean success = orderList.remove(order);
		System.out.println(success + ":" + orderList.size());
		return null;
	}

	public String editAction(Order order) {
		order.setEditable(true);
		return null;
	}

	public String saveAction(Order order) {
		order.setEditable(false);
		return null;
	}

	public String saveAllAction() {
		for (Order order : orderList) {
			order.setEditable(false);
		}
		return null;
	}

	public boolean isSortAscending() {
		return sortAscending;
	}

	public void setSortAscending(boolean sortAscending) {
		this.sortAscending = sortAscending;
	}
	
	public SortableDataModel<Order> getSortableDataModel() {
		return sortableDataModel;
	}

	public void setSortableDataModel(SortableDataModel<Order> sortableDataModel) {
		this.sortableDataModel = sortableDataModel;
	}
}
